﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Web.Core
{
    /// <summary>
    /// session方式序列化
    /// </summary>
    public class SessionSerialization : ISerialization
    {
        /// <summary>
        /// 登陆信息持久化,机制httpCookies
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="username"></param>
        /// <param name="email"></param>
        /// <param name="isSave"></param>
        public void Serialization(int userid, string username, string email, bool isSave)
        {
            System.Web.HttpContext.Current.Session["UserID"] = userid.ToString();
            System.Web.HttpContext.Current.Session["UserName"] = username.ToString();
            System.Web.HttpContext.Current.Session["Email"] = email.ToString();
        }
        /// <summary>
        /// 登陆信息持久化,机制httpCookies
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="username"></param>
        /// <param name="email"></param>
        public void Serialization(int userid, string username, string email)
        {
            Serialization(userid, username, email, false);
        }

        public int UserID
        {
            get
            {
                int _userid;
                int.TryParse((System.Web.HttpContext.Current.Session["UserID"] ?? string.Empty).ToString(), out _userid);
                return _userid;
            }
        }

        public string UserName
        {
            get
            {
                return (System.Web.HttpContext.Current.Session["UserName"] ?? string.Empty).ToString();
            }
        }

        public string Email
        {
            get
            {
                return (System.Web.HttpContext.Current.Session["Email"] ?? string.Empty).ToString();
            }
        }

        public bool IsLogin
        {
            get
            {
                return UserID > 0;
            }
        }

        public void Clear()
        {
            System.Web.HttpContext.Current.Session.Clear();
        }
    }
}
